import { Request, Response } from 'express';
import mockjs from 'mockjs';

export default {
  // 支持值为 Object 和 Array
  'GET /api/users': (req: Request, res: Response) => {
    const data = mockjs.mock({
      'data|100': [{ name: '@cname', 'value|1-100': 50, 'type|0-2': 1 }],
    });
    res.json({
      data: data,
      code: 0,
      msg: 'success',
    });
  },

  // GET 可忽略
  '/api/users/1': { id: 1 },

  // 支持自定义函数，API 参考 express@4
  'POST /api/users/create': (req: Request, res: Response) => {
    // 添加跨域请求头
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.end('ok');
  },

  'POST /api/users/login': (req: Request, res: Response) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    if (Math.random() > 0.5) {
      res.json({ code: 0 });
    } else res.json({ code: 1 });
  },
};
